package com.example.demo.web.autoidempotent;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class BusinessController {

    @Resource
    private TokenService tokenService;

    @Autowired
    private TestService testService;

    private static final String CODE_SUCCESS = "success";

    @PostMapping("/get/token")
    public String getToken() {
        String token = tokenService.createToken();
        if (StringUtils.isNotEmpty(token)) {
            ResultVo resultVo = new ResultVo();
            resultVo.setCode("100");
            resultVo.setMessage(CODE_SUCCESS);
            resultVo.setData(token);
            return JSONObject.toJSONString(resultVo);
        }
        return StringUtils.EMPTY;
    }


    @AutoIdempotent
    @PostMapping("/test/Idempotence")
    public String testIdempotence() {
        String businessResult = testService.testIdempotence();
        System.out.println("/test/Idempotence=testIdempotence");
        if (StringUtils.isNotEmpty(businessResult)) {
            return businessResult;
        }
        return StringUtils.EMPTY;
    }
}

